/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.io.IOException;
import java.net.URL;

import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.JTextPane;
import javax.swing.JButton;

import org.apache.log4j.Logger;

import who.tmap.common.Constants;
import javax.swing.JScrollPane;

/**
 * A dialog to display TMap Designer license
 * @author stl
 *
 */
public class JDialogLicense extends JDialog {

    private static Logger logger = Logger.getLogger( JDialogLicense.class );	
	
	private JPanel jContentPane = null;
	private JTextPane jTextPaneLicense = null;
	private JButton jButtonClose = null;
	private JPanel jPanelButton = null;

	private JScrollPane jScrollPane = null;

	/**
	 * This is the default constructor
	 */
	public JDialogLicense( Frame parent, boolean modal ) {
		super( parent, modal );
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(600, 400);
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle("TMap Designer License");
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo(null);
		
		getRootPane().setDefaultButton( getJButtonClose() );
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanelButton(), java.awt.BorderLayout.SOUTH);
			jContentPane.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextPaneLicense	
	 * 	
	 * @return javax.swing.JTextPane	
	 */
	private JTextPane getJTextPaneLicense() {
		if (jTextPaneLicense == null) {
			jTextPaneLicense = new JTextPane();
			URL url = getClass().getResource(Constants.LICENSE_FILE);
			try {
				jTextPaneLicense.setPage(url);
			} catch (IOException e) {
				logger.error( e );
			}		
			jTextPaneLicense.setEditable( false );
			
		}
		return jTextPaneLicense;
	}

	/**
	 * This method initializes jButtonClose	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonClose() {
		if (jButtonClose == null) {
			jButtonClose = new JButton();
			jButtonClose.setText("Close");
			jButtonClose.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return jButtonClose;
	}

	/**
	 * This method initializes jPanelButton	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelButton() {
		if (jPanelButton == null) {
			jPanelButton = new JPanel();
			jPanelButton.add(getJButtonClose(), null);
		}
		return jPanelButton;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTextPaneLicense());
		}
		return jScrollPane;
	}

}
